const express = require('express');
const router = express.Router();
const qiniu = require('qiniu');

ApiClient = require('../index.js').ApiClient;
var client = new ApiClient({
    appkey: '27719267',
    appsecret: 'ae64126fba56b544d0c4477df34b7680',
    url: 'https://gw.api.taobao.com/router/rest',
});

router.get('/', (req, res) => {
    res.send(`<h1>hello taobao Server!!</h1>`);
});

router.get('/qiniuToken', (req, res) => {
    const accessKey = 'LJDVwpA0PLwBIxTG49GxCx6mheXrxMGOY39idUGF';
    const secretKey = 'wbs_55Zeo5xkBHsSkGsubDgJ2KuVMPT12QH-ukq0';
    const bucket = 'zhuyaogeng';

    let mac = new qiniu.auth.digest.Mac(accessKey, secretKey);
    var options = {
        scope: bucket,
    };

    var putPolicy = new qiniu.rs.PutPolicy(options);
    var uploadToken = putPolicy.uploadToken(mac);
    // return this.jsonp({ 'token': uploadToken })
    res.send({
        token: uploadToken,
    });
});

router.get('/taobao/getTime', (req, res) => {
    client.execute(
        'taobao.time.get',
        {
            fields: 'yaogeng_zhu0608,1',
        },
        function (error, response) {
            if (!error) console.log(response);
            res.send(response);
        }
    );
});

/**
 * 获取淘宝活动信息
 */
router.get('/taobao/get_activityInfo', (req, res) => {
    client.execute(
        'taobao.tbk.activity.info.get',
        {
            adzone_id: '110809800144',
            relation_id: '1',
            sub_pid: 'mm_130446138_264200278_74098500443',
            activity_material_id: '20150318020007193',
            union_id: 'taobaoserver',
        },
        function (error, response) {
            if (!error) console.log(response);
            res.send(response)
        }
    );
});

router.get('/taobao/command', (req, res) => {
    client.execute('taobao.tbk.tpwd.create', {
        'url': 'https://s.click.taobao.com/znbUOeu'
    }, function(error, response) {
        if (error) {
            return res.send(error)
        }
        res.send(response)
    })
})
// 淘宝店铺搜索
router.get('/taobao/tbk/shop/get', (req, res) => {
    let data = req.query;
    client.executeWithHeader(
        'taobao.tbk.shop.get',
        {
            fields: 'user_id,shop_title,shop_type,seller_nick,pict_url,shop_url',
            q: data.kw,
        },
        {},
        function (error, response) {
            if (!error)
                // console.log(response);
                res.send(response);
            else console.log(error);
        }
    );
});

// 获取所有选品库的信息
router.get('/taobao/itempropvalues/get', (req, res) => {
    client.executeWithHeader(
        'taobao.tbk.uatm.favorites.get',
        {
            fields: 'favorites_title,favorites_id,type',
            // 'nick': 'yaogeng_zhu520',
        },
        {},
        function (error, response) {
            if (!error)
                // console.log(response);
                res.send(response);
            // console.log(error);
            else res.send(error);
        }
    );
});

// 获取女装的top200接口
router.get('/2/get_women_clothing', (req, res) => {
    let data = req.query;
    /***
     *  自定义参数：
     *  platform： 2  //  链接形式：1：PC，2：无线，默认：１
     *  page_size: 20	 //页大小，默认20，1~100
     *  page_no: 1   // 第几页，默认：1，从1开始计数
     *
     */
    client.executeWithHeader(
        'taobao.tbk.uatm.favorites.item.get',
        {
            fields: 'num_iid,title,pict_url,small_images,reserve_price,zk_final_price,user_type,provcity,item_url,seller_id,volume,nick,shop_title,zk_final_price_wap,event_start_time,event_end_time,tk_rate,status,type',
            adzone_id: '74098500443',
            favorites_id: '19716228',
            platform: data.platform || 2, // 默认是无线
            page_no: data.page_no || 1, // 默认页数数1
            page_size: data.page_size || 20, // 默认me
        },
        {},
        function (error, response) {
            if (!error)
                // console.log(response);
                res.send(response);
            // console.log(error);
            else res.send(error);
        }
    );
});

// 获取男装的top200接口
router.get('/2/get_man_clothing', (req, res) => {
    // console.log(req.query)
    let data = req.query;
    // console.log(req)
    client.executeWithHeader(
        'taobao.tbk.uatm.favorites.item.get',
        {
            fields: 'num_iid,title,pict_url,small_images,reserve_price,zk_final_price,user_type,provcity,item_url,seller_id,volume,nick,shop_title,zk_final_price_wap,event_start_time,event_end_time,tk_rate,status,type',
            adzone_id: '74098500443',
            favorites_id: '19716227',
            platform: data.platform || 2, // 默认是无线
            page_no: data.page_no || 1, // 默认页数数1
            page_size: data.page_size || 20, // 默认me
        },
        {},
        function (error, response) {
            if (!error)
                // console.log(response);
                res.send(response.results);
            // console.log(error);
            else res.send(error);
        }
    );
});

// 热搜榜
router.get('/2/get_hot', (req, res) => {
    // console.log(req.query)
    let data = req.query;
    // console.log(req)
    // console.log(req)
    client.executeWithHeader(
        'taobao.tbk.uatm.favorites.item.get',
        {
            fields: 'num_iid,title,pict_url,small_images,reserve_price,zk_final_price,user_type,provcity,item_url,seller_id,volume,nick,shop_title,zk_final_price_wap,tk_rate,status,type',
            adzone_id: '74098500443',
            favorites_id: '19716251',
            platform: data.platform || 2, // 默认是无线
            page_no: data.page_no || 1, // 默认页数数1
            page_size: data.page_size || 20, // 默认me
        },
        {},
        function (error, response) {
            if (!error)
                // console.log(response);
                res.send(response.results);
            // console.log(error);
            else res.send(error);
        }
    );
});

// 运动用品
router.get('/2/get_sport', (req, res) => {
    // console.log(req.query)
    let data = req.query;
    // console.log(req)
    client.executeWithHeader(
        'taobao.tbk.uatm.favorites.item.get',
        {
            fields: 'num_iid,title,pict_url,small_images,reserve_price,zk_final_price,user_type,provcity,item_url,seller_id,volume,nick,shop_title,zk_final_price_wap,event_start_time,event_end_time,tk_rate,status,type',
            adzone_id: '74098500443',
            favorites_id: '19716249',
            platform: data.platform || 2, // 默认是无线
            page_no: data.page_no || 1, // 默认页数数1
            page_size: data.page_size || 20, // 默认me
        },
        {},
        function (error, response) {
            if (!error)
                // console.log(response);
                res.send(response.results);
            // console.log(error);
            else res.send(error);
        }
    );
});

//内衣 underskirt
router.get('/2/get_underskirt', (req, res) => {
    // console.log(req.query)
    let data = req.query;
    // console.log(req)
    client.executeWithHeader(
        'taobao.tbk.uatm.favorites.item.get',
        {
            fields: 'num_iid,title,pict_url,small_images,reserve_price,zk_final_price,user_type,provcity,item_url,seller_id,volume,nick,shop_title,zk_final_price_wap,event_start_time,event_end_time,tk_rate,status,type',
            adzone_id: '74098500443',
            favorites_id: '19716246',
            platform: data.platform || 2, // 默认是无线
            page_no: data.page_no || 1, // 默认页数数1
            page_size: data.page_size || 20, // 默认me
        },
        {},
        function (error, response) {
            if (!error)
                // console.log(response);
                res.send(response.results);
            // console.log(error);
            else res.send(error);
        }
    );
});

//家居用品 home_product
router.get('/2/get_home_product', (req, res) => {
    // console.log(req.query)
    let data = req.query;
    // console.log(req)
    client.executeWithHeader(
        'taobao.tbk.uatm.favorites.item.get',
        {
            fields: 'num_iid,title,pict_url,small_images,reserve_price,zk_final_price,user_type,provcity,item_url,seller_id,volume,nick,shop_title,zk_final_price_wap,event_start_time,event_end_time,tk_rate,status,type',
            adzone_id: '74098500443',
            favorites_id: '19716245',
            platform: data.platform || 2, // 默认是无线
            page_no: data.page_no || 1, // 默认页数数1
            page_size: data.page_size || 20, // 默认me
        },
        {},
        function (error, response) {
            if (!error)
                // console.log(response);
                res.send(response.results);
            // console.log(error);
            else res.send(error);
        }
    );
});

//数码家电  Home_Appliance 19716243
router.get('/2/get_home_appliance', (req, res) => {
    // console.log(req.query)
    let data = req.query;
    // console.log(req)
    client.executeWithHeader(
        'taobao.tbk.uatm.favorites.item.get',
        {
            fields: 'num_iid,title,pict_url,small_images,reserve_price,zk_final_price,user_type,provcity,item_url,seller_id,volume,nick,shop_title,zk_final_price_wap,event_start_time,event_end_time,tk_rate,status,type',
            adzone_id: '74098500443',
            favorites_id: '19716243',
            platform: data.platform || 2, // 默认是无线
            page_no: data.page_no || 1, // 默认页数数1
            page_size: data.page_size || 20, // 默认me
        },
        {},
        function (error, response) {
            if (!error)
                // console.log(response);
                res.send(response.results);
            // console.log(error);
            else res.send(error);
        }
    );
});

// 美妆 mark_up 19716241
router.get('/2/get_mark_up', (req, res) => {
    // console.log(req.query)
    let data = req.query;
    // console.log(req)
    client.executeWithHeader(
        'taobao.tbk.uatm.favorites.item.get',
        {
            fields: 'num_iid,title,pict_url,small_images,reserve_price,zk_final_price,user_type,provcity,item_url,seller_id,volume,nick,shop_title,zk_final_price_wap,event_start_time,event_end_time,tk_rate,status,type',
            adzone_id: '74098500443',
            favorites_id: '19716241',
            platform: data.platform || 2, // 默认是无线
            page_no: data.page_no || 1, // 默认页数数1
            page_size: data.page_size || 20, // 默认me
        },
        {},
        function (error, response) {
            if (!error)
                // console.log(response);
                res.send(response.results);
            // console.log(error);
            else res.send(error);
        }
    );
});

// 食品 foods 19716240
router.get('/2/get_foods', (req, res) => {
    // console.log(req.query)
    let data = req.query;
    // console.log(req)
    client.executeWithHeader(
        'taobao.tbk.uatm.favorites.item.get',
        {
            fields: 'num_iid,title,pict_url,small_images,reserve_price,zk_final_price,user_type,provcity,item_url,seller_id,volume,nick,shop_title,zk_final_price_wap,event_start_time,event_end_time,tk_rate,status,type',
            adzone_id: '74098500443',
            favorites_id: '19716240',
            platform: data.platform || 2, // 默认是无线
            page_no: data.page_no || 1, // 默认页数数1
            page_size: data.page_size || 20, // 默认me
        },
        {},
        function (error, response) {
            if (!error)
                // console.log(response);
                res.send(response.results);
            // console.log(error);
            else res.send(error);
        }
    );
});

// 鞋包配饰  accessories 19716239
router.get('/2/get_accessories', (req, res) => {
    // console.log(req.query)
    let data = req.query;
    // console.log(req)
    client.executeWithHeader(
        'taobao.tbk.uatm.favorites.item.get',
        {
            fields: 'num_iid,title,pict_url,small_images,reserve_price,zk_final_price,user_type,provcity,item_url,seller_id,volume,nick,shop_title,zk_final_price_wap,event_start_time,event_end_time,tk_rate,status,type',
            adzone_id: '74098500443',
            favorites_id: '19716239',
            platform: data.platform || 2, // 默认是无线
            page_no: data.page_no || 1, // 默认页数数1
            page_size: data.page_size || 20, // 默认me
        },
        {},
        function (error, response) {
            if (!error)
                // console.log(response);
                res.send(response.results);
            // console.log(error);
            else res.send(error);
        }
    );
});

// 图文物料图
router.get('/2/get_banner', (req, res) => {
    // console.log(req.query)
    let data = req.query;
    // console.log(req)
    client.executeWithHeader(
        'taobao.tbk.content.get',
        {
            adzone_id: '74098500443',
            type: data.type || 1, // 内容类型，1:图文、2: 图集、3: 短视频
            count: data.count || 4, // 希望获取的条数
        },
        {},
        function (error, response) {
            if (!error)
                // console.log(response);
                res.send(response);
            // console.log(error);
            else res.send(error);
        }
    );
});

// 根据搜索词搜索店铺
router.get('/2/search_shops', (req, res) => {
    let data = req.query;
    console.log(data);
    client.executeWithHeader(
        'taobao.tbk.shop.get',
        {
            fields: 'user_id,shop_title,shop_type,seller_nick,pict_url,shop_url',
            q: data.kw, // 搜索的关键字
            platform: data.platform || 2, // 链接形式：1：PC，2：无线，默认：１
            page_no: data.page || 1, // 默认为1页
            page_size: data.page_size || 20, // 默认为20条
        },
        {},
        function (error, response) {
            if (!error)
                // console.log(response);
                res.send(response);
            // console.log(error);
            else res.send(error);
        }
    );
});

// 根据搜索词搜索宝贝信息
router.get('/2/search_goods', (req, res) => {
    let data = req.query;
    console.log(data);
    client.executeWithHeader(
        'taobao.tbk.dg.material.optional',
        {
            adzone_id: '74098500443',
            q: data.kw, // 搜索的关键字
            platform: data.platform || 2, // 链接形式：1：PC，2：无线，默认：１
            page_no: data.page || 1, // 默认为1页
            page_size: data.page_size || 20, // 默认为20条
        },
        {},
        function (error, response) {
            if (!error)
                // console.log(response);
                res.send(response);
            // console.log(error);
            else res.send(error);
        }
    );
});

// 获取淘宝系统时间
router.get('/2/getTime', (req, res) => {
    client.execute('taobao.time.get', {}, function (error, response) {
        if (!error) res.send(response);
        else console.log(error);
    });
});

// 公用淘口令
router.get('/2/get/tpwd', (req, res) => {
    client.execute(
        'taobao.tbk.tpwd.create',
        {
            user_id: '123',
            text: '长度大于5个字符',
            url: 'https://uland.taobao.com/',
            logo: 'https://uland.taobao.com/',
            ext: '{}',
        },
        function (error, response) {
            if (!error) res.send(response);
            else res.send(error);
        }
    );
});

//
module.exports = router;
